# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=py3-twisted
pkgver=22.2.0
pkgrel=0
pkgdesc="Asynchronous networking framework written in Python3"
url="https://twistedmatrix.com/"
arch="all"
license="MIT"
depends="
	py3-cryptography
	py3-zope-interface
	py3-constantly
	py3-incremental>=21.3.0
	py3-automat
	py3-hyperlink
	py3-attrs
	py3-typing-extensions
	py3-pyhamcrest
	py3-service_identity
	py3-idna
	py3-pyserial
	py3-h2
	"
makedepends="libtirpc-dev py3-setuptools python3-dev cython"
checkdepends="xvfb-run py3-appdirs tzdata py3-asn1 py3-bcrypt"
subpackages="$pkgname-doc"
source="https://github.com/twisted/twisted/archive/twisted-$pkgver.tar.gz
	disable-test-missing-ckeygen.patch
	fix-test-import.patch
	remove-failing-test.patch
	"
builddir="$srcdir"/twisted-twisted-$pkgver

replaces="py-twisted" # Backwards compatibility
provides="py-twisted=$pkgver-r$pkgrel" # Backwards compatibility

case "$CARCH" in
	s390x) options="$options !check" # Builders don't have IPV6 ;;
esac

# secfixes:
#   22.2.0-r0:
#     - CVE-2022-21716
#   22.1.0-r0:
#     - CVE-2022-21712
#   20.3.0-r0:
#     - CVE-2020-10108
#     - CVE-2020-10109
#   19.10.0-r0:
#     - CVE-2019-9512
#     - CVE-2019-9514
#     - CVE-2019-9515
#   19.7.0-r0:
#     - CVE-2019-12387
#     - CVE-2019-12855
#   16.4.0-r0:
#     - CVE-2016-1000111

prepare() {
	default_prepare
	# Remove tests that always fail due to our not-upstream testing procedure
	# these tests if invoking the Twisted binary and twisted.trial tests work
	rm -f src/twisted/test/test_main.py
}

build() {
	find . -name '*.pyx' -exec cython {} \;
	CFLAGS="$CFLAGS $(pkgconf --cflags libtirpc)" python3 setup.py build
}

check() {
	PYTHONPATH="$(echo $PWD/build/lib*)" xvfb-run python3 \
		-c 'from twisted.scripts.trial import run; run()' twisted
}

package() {
	python3 setup.py install --prefix=/usr --root="$pkgdir"
	# Do not remove the test as it is a legitimate module and not
	# a (common) packaging mistake by upstream
	# rm -rf "$pkgdir"/usr/lib/python3*/site-packages/twisted/test

	_mans="pyhtmlizer.1 trial.1 twistd.1"
	for _man in $_mans; do
		install -m644 -D "$builddir"/docs/core/man/$_man \
			"$pkgdir"/usr/share/man/man1/$_man
	done
}

doc() {
	replaces="py-twisted-doc" # Backwards compatibility
	default_doc
}

sha512sums="
a2f66055f0ea6658a04888316b9eb90bd0f389c72db8315fe1722a70f16bb290805d98acc8cfd8a5d22b4817683603aaea50bb04a204927898bd74fc58a1db6d  twisted-22.2.0.tar.gz
c432bbc2098bf7e36aaa51a548a5d60de4f790caf10fc9dfa04e11e88b4fbea4c4245106c1152ce4f450082dd8111d5f72b65c5c99ee2d0e5c8e0511ae821b6c  disable-test-missing-ckeygen.patch
a8e073ff8a35ad8faa95d023e9cf72dc3af0f0e4386fc0e66e57552eb9ff9ae1648b7c0809a5fbb6dca5799db7983280aefcabad1b52bf1187f25b62a4fe39a1  fix-test-import.patch
eeaf81c9599930f8645c8bf14baa25c6f66aaf3d396266124b825c8e672082ecab9d2cf5aa5d3be9c2a81d4c51afd7de1f68ee3699c773c406d7febea59230e1  remove-failing-test.patch
"
